﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.Materials;
using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.ProcurementsCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.ProcurementsCommandCommand
{
    /// <summary>
    /// 采购发票修改命令处理
    /// </summary>
    public class UpdPurchaseInvoicesHandler : IRequestHandler<UpdPurchaseInvoicesCommand, APIResult<object>>
    {
        /// <summary>
        /// 仓储
        /// </summary>
        private readonly IBaseRepository<PurchaseInvoice> _purchaseInvoice;
        //private readonly IBaseRepository<Material_BasicModel> _material_BasicModel;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 异常记录
        /// </summary>
        private readonly ILogger<UpdPurchaseInvoicesHandler> logger;

        public UpdPurchaseInvoicesHandler(IBaseRepository<PurchaseInvoice> purchaseInvoice, IMapper mapper, ILogger<UpdPurchaseInvoicesHandler> logger)
        {
            _purchaseInvoice = purchaseInvoice;
            this.mapper = mapper;
            this.logger = logger;
            // _material_BasicModel = material_BasicModel;
        }

        public Task<APIResult<object>> Handle(UpdPurchaseInvoicesCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result=new APIResult<object>();
            try
            {
                result.Code = APIEnum.修改成功;
                result.Msg = APIEnum.修改成功.ToString();
                var entity = mapper.Map<PurchaseInvoice>(request);
                _purchaseInvoice.Update(entity);


                //_material_BasicModel.Update(basicmodel);

                result.data = entity;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "处理请求时发生错误");
                throw;
            }
            return Task.FromResult(result);
        }
    }
}
